package com.zkny.zknyerp.controller;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zkny.zknyerp.feign.UserFeign;
import com.zkny.zknyerp.model.User;
import com.zkny.zknyerp.utils.Constant;
import com.zkny.zknyerp.utils.RedisUtils;
import com.zkny.zknyerp.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;

/**
 * 用户对外接口
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class UserController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserFeign userFeign;

    @Value("${zhky.username}")
    private String zhkyUsername;
    @Value("${zhky.password}")
    private String zhkyPassword;

    /**
     * 用户登录接口
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        //获取前端传过来的token
        UsernamePasswordToken passwordToken = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        //得到用户主体
        Subject subject = SecurityUtils.getSubject();
        //将数据交给shiro验证,若有异常，由全局异常捕获并返回前端，反之自己返回
        subject.login(passwordToken);
        //获取token，即sessionid
        String token1 = subject.getSession().getId().toString();
        //获取user信息
        User curUser = (User) subject.getPrincipal();


        //调用智慧康养接口获取TOKEN
        User user1 = new User();
        user1.setUsername(zhkyUsername);
        user1.setPassword(zhkyPassword);
        String str = userFeign.login(user1);
        JSONObject jsonObject = JSON.parseObject(str);
        JSONObject data = jsonObject.getJSONObject("data");
        String token = data.getString("token");

        //封装前端用户信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("token1", token1);
        map.put("token", token);
        map.put("username", curUser.getName());
        //增加一条流水信息
        return  new Result(Constant.code_0000, Constant.msg_0000, map);
    }

    /*注销登陆
     * */
    @GetMapping("/logout")
    public Result logout() {
        /*要从定向得到shiro的注销方法去*/
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String sessionid = session.getId().toString();
        String key = "shiro:session:" + sessionid;
        if(redisUtils.hasKey(key)){
            redisUtils.del(key);
            return new Result(Constant.code_0000, Constant.msg_0000);
        }
        return new Result(Constant.code_9999, Constant.msg_9999);

    }


    /**
     * 验证用户登录状态
     * @return
     */
    @PostMapping("/checkLogin")
    public Result checkLogin(){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return new Result(Constant.code_1014, Constant.msg_1014);
        }else {
            return new Result(Constant.code_1015, Constant.msg_1015);
        }
    }
}
